package com.example.howard.xiao7music.ui.Activity.RemoteActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.howard.xiao7music.MyApplication;
import com.example.howard.xiao7music.R;
import com.example.howard.xiao7music.presenter.RemoteSongPresenter;
import com.example.howard.xiao7music.service.MusicPlayerService;
import com.example.howard.xiao7music.ui.BottomPlayer;
import com.example.howard.xiao7music.ui.adapter.RemoteAdapter.RankingListAdapter;

import java.util.List;
import java.util.Map;

public class RankingListActivity extends Activity {

    private AdapterView.OnItemClickListener listener;
    private RemoteSongPresenter remoteSongPresenter;
    private MyApplication myApplication;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private RankingListAdapter myAdapter;
    private TextView title;
    private ListView listView;
    private BottomPlayer bottomPlayer;




    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ranking_list);
        myApplication= (MyApplication) getApplication();
        remoteSongPresenter=new RemoteSongPresenter(new RemoteSongPresenter.callback() {
            @Override
            public void getRankingSongList(List<Map<String, Object>> songlist) {

            }

            @Override
            public void getSearchBySinger(List<Map<String, Object>> songlist) {

            }

            @Override
            public void getSearchBySong(List<Map<String, Object>> songlist) {

            }
        });
        init();
        refreshListView();
        bottomPlayer=new BottomPlayer();
        fragmentManager=getFragmentManager();
        transaction=fragmentManager.beginTransaction();
        transaction.add(R.id.ranking_bottomplaybar,bottomPlayer);
        transaction.commit();

    }

    private void init(){

        title= (TextView) findViewById(R.id.ranking_titel);
        listView= (ListView) findViewById(R.id.ranking_list);
        title.setText("百度音乐排行榜");

    }

    private void refreshListView(){
        myAdapter=new RankingListAdapter(this);
        listView.setAdapter(myAdapter);
        listener=resetClickListener();
        listView.setOnItemClickListener(listener);
    }

    private AdapterView.OnItemClickListener resetClickListener(){
        AdapterView.OnItemClickListener songListclickListener = new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {

                Intent intent=new Intent(RankingListActivity.this,RankingSongActivity.class);
                intent.putExtra("num",arg2);
                startActivity(intent);
            }
        };
        return songListclickListener;
    }

    @Override
    protected void onResume() {
        super.onResume();
        getBottomPlayerState(bottomPlayer);
    }

    @Override
    protected void onPause() {
        super.onPause();
        setBottomPlayerState(bottomPlayer.getMusicPlayerService(),bottomPlayer.getSongList(),
                bottomPlayer.getMap(),bottomPlayer.getPosition());
    }

    //获取bottomplayer的状态
    private void getBottomPlayerState (BottomPlayer player){
        if (myApplication.musicPlayerService!=null){
            player.setMusicPlayerService(myApplication.musicPlayerService);
        }
        if (myApplication.songdatalist.size()>0){
            player.setSongList(myApplication.songdatalist);
            player.setMap(myApplication.map);
            player.setPosition(myApplication.position);
        }

    }

    //上传bottomplayer的状态
    private void setBottomPlayerState (MusicPlayerService service, List<Map<String, Object>> songlist,
                                       Map<String, Object> map, int pos){
        myApplication.setMusicPlayerService(service);
        myApplication.setSongdatalist(songlist);
        myApplication.setMap(map);
        myApplication.setPosition(pos);
    }

}
